// ignore_for_file: non_constant_identifier_names

import 'package:get/get.dart';
import 'package:sj001/model/ChatMsgItem.dart';
import 'package:sj001/model/revMsgModel.dart';
import 'package:sj001/utils/Utils.dart';
class ConversationMessageFeedback {
  String? rating;
  ConversationMessageFeedback({
    this.rating,
  });

  Map<String, dynamic> toJson() {
    return {
      "rating": rating,
    };
  }

  factory ConversationMessageFeedback.fromJson(Map<String, dynamic> json) {
    return ConversationMessageFeedback(
      rating: json["rating"],
    );
  }

  bool get isLike {
    return rating == 'like';
  }
}

class ConversationMessageItem {
  String? id;
  String? conversation_id;
  String? parent_message_id;
  Map<String, dynamic>? inputs;
  String? query;
  String? answer;
  List? message_files;
  ConversationMessageFeedback? feedback;
  List? retriever_resources;
  int? created_at;
  List? agent_thoughts;
  String? status;
  String? error;

  ConversationMessageItem({
    this.id,
    this.conversation_id,
    this.parent_message_id,
    this.inputs,
    this.query,
    this.answer,
    this.message_files,
    this.feedback,
    this.retriever_resources,
    this.created_at,
    this.agent_thoughts,
    this.status,
    this.error,
  });

  Map<String, dynamic> toJson() {
    return {
      "id": id,
      "conversation_id": conversation_id,
      "parent_message_id": parent_message_id,
      "inputs": inputs,
      "query": query,
      "answer": answer,
      "message_files": message_files,
      "feedback": feedback?.toJson(),
      "retriever_resources": retriever_resources,
      "created_at": created_at,
      "agent_thoughts": agent_thoughts,
      "status": status,
      "error": error,
    };
  }

  factory ConversationMessageItem.fromJson(Map<String, dynamic> json) {
    return ConversationMessageItem(
      id: json["id"],
      conversation_id: json["conversation_id"],
      parent_message_id: json["parent_message_id"],
      inputs: json["inputs"],
      query: json["query"],
      answer: json["answer"],
      message_files: json["message_files"],
      feedback: json["feedback"] == null ? null : ConversationMessageFeedback.fromJson(json['feedback']),
      retriever_resources: json["retriever_resources"],
      created_at: json["created_at"],
      agent_thoughts: json["agent_thoughts"],
      status: json["status"],
      error: json["error"],
    );
  }
}

class ConversationMessageModel {
  int? limit;
  bool? has_more;
  List<ConversationMessageItem>? data;

  ConversationMessageModel({this.limit, this.has_more, this.data});

  Map<String, dynamic> toJson() {
    return {
      "limit": limit,
      "has_more": has_more,
      "data": data?.map((e) => e.toJson()).toList(),
    };
  }

  factory ConversationMessageModel.fromJson(Map<String, dynamic> json) {
    List<ConversationMessageItem>? data;

    if (json['data'] != null) {
      data = [];
      for (var e in json['data']) {
        data.add(ConversationMessageItem.fromJson(e));
      }
    }

    return ConversationMessageModel(
      limit: json["limit"],
      has_more: json["has_more"],
      data: data,
    );
  }

  RxList<Rx<ChatMsgItem>> trans2ChatMsgItem() {
    RxList<Rx<ChatMsgItem>> list = <Rx<ChatMsgItem>>[].obs;
    for (ConversationMessageItem item in data ?? []) {
      Rx<ChatMsgItem> queryItem = ChatMsgItem(
        user: 1,
        message_id: (item.id ?? '').obs,
        createAt: (item.created_at ?? 0).obs,
        models: [
          RevMsgModel(
            event: '',
            conversation_id: item.conversation_id,
            message_id: item.id,
            created_at: item.created_at,
            task_id: '',
            id: item.id,
            answer: item.query,
          ),
        ].obs,
        isDone: true.obs,
        conversation_id: item.conversation_id ?? '',
        isLike: false.obs,
      ).obs;

      Rx<ChatMsgItem> answerItem = ChatMsgItem(
        createAt: (item.created_at ?? 0).obs,
        user: 0,
        message_id: (item.id ?? '').obs,
        models: [
          RevMsgModel(
            event: '',
            conversation_id: item.conversation_id,
            message_id: item.id,
            created_at: item.created_at,
            task_id: '',
            id: item.id,
            answer: item.answer,
          ),
        ].obs,
        isDone: true.obs,
        conversation_id: item.conversation_id ?? '',
        isLike: (item.feedback?.isLike ?? false).obs,
      ).obs;

      list.add(queryItem);
      list.add(answerItem);
    }
    return list;
  }
}
